<template>
	<view class="table">
		<view class="table-header">
			<view class="tr flex">
				<view class="td" v-for="(it, index) in columns" :key="index" :style="[setTdWidth(it.width)]">
					{{ it.text }}
				</view>
			</view>
		</view>
		<scroll-view scroll-y enable-flex class="table-scroll" :refresher-enabled="refresherEnabled"
			:refresher-triggered="isPull" @refresherrefresh="onPull" @scrolltolower="loadMore">
			<u-empty icon="http://cdn.uviewui.com/uview/empty/data.png" v-if="tableData.length === 0"></u-empty>
			<view class="table-body" v-else>
				<view class="tr flex line" v-for="(it, index) in tableData" :key="index" @tap="toPage(it)">
					<view class="td" v-for="(key, kIndex) in columns" :key="kIndex" :style="[setTdWidth(key.width)]">
						<template v-if="key.slot">
							<slot :name="key.name" :rows="it" :index="index" />
						</template>
						<template v-else> {{ it[key.name] }}</template>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		name: "Table",
		props: {
			columns: {
				type: Array,
				default: () => []
			},
			tableData: {
				type: Array,
				default: () => []
			},
			// 开启下拉刷新
			refresherEnabled: {
				type: Boolean,
				default: false
			},
			// 数据加载中
			loading: {
				type: Boolean,
				default: false
			},
			// 数据是否已全部加载完毕
			finished: {
				type: Boolean,
				default: false
			},
			finishedText: {
				type: String,
				default: "没有更多了"
			},
			value: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				isPull: false,
				isLoading: false
			}
		},
		watch: {
			value(n) {
				this.isLoading = n
			}
		},
		methods: {
			setTdWidth(width) {
				if (!width) return "";
				return `width:${width}`
			},
			// 下拉
			onPull() {
				this.isPull = true;
				this.$emit("refresherrefresh", () => this.isPull = false)
			},
			// 上拉加载
			loadMore() {
				if (this.finished) return;
				if (this.isLoading) return;
				/* if (props.isInternalLoading && props.req) {
					isLoading.value = true;
					props.req().then(res => {
						const { status, code, data } = res.data;
						isLoading.value = false;
						listData.value = listData.value.concat(data);
						emit("scrolltolower");
					}).catch(err => {
						isLoading.value = false;
						console.error(err);
					});
					return;
				} */
				this.isLoading = true;
				this.$emit("scrolltolower")
				this.$emit("input", this.isLoading)

			},
			toPage(it) {
				if (!it.routePath && !it.routeUrl) return;
				let url = it.routePath ? it.routePath : `/pages/webview/index?src=${it.routeUrl}`
				this.nav(url)
			}
		}
	}
</script>

<style scoped lang="scss">
	.flex {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.line {
		border-top: 1rpx solid #DFE3EA;
	}

	.red {
		color: #f00;
	}

	.table {
		border-radius: 20rpx;
		background: #EFF5FF;
		height: 100%;

		&-header {
			// height: 65rpx;
			// line-height: 65rpx;
			font-size: 30rpx;
			font-weight: bold;
		}

		&-body {
			.td {
				font-size: 26rpx;
			}
		}

		&-scroll {
			height: calc(100% - 80rpx)
		}

		.td {
			flex: 1;
			// line-height: 80rpx;
			padding: 20rpx 10rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: center;
			min-height: 80rpx;
			color: #333;
			text-align: center;
		}

	}
</style>